﻿Public Class UCKhachHang
    Sub GridView_Load()
        GControl.DataSource = KhachHangBUS.KhachHang_Load()
    End Sub

    Function TestInfo() As Boolean
        If txtHoTen.Text = "" Or txtCMND.Text = "" Or txtSDT.Text = "" Or txtNgaythuenha.Text = "" Or txtNgayketthuc.Text = "" Then
            ThongBao.TestInfo()
            Return False
        End If
        Return True
    End Function
    Sub ddlNhaLoad() ' Load nhung loai nha ma KH chua dang ki
        Dim sql As String = "select * from NHA where TINHTRANG = 0"
        ddlNha.DataSource = DataProvider.LoadTable(sql)
        ddlNha.DisplayMember = "MANHA"
        ddlNha.ValueMember = "MANHA"
    End Sub

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        If Not TestInfo() Then
            Return
        End If
        If ddlNha.Text = "" Then
            MessageBox.Show("Đã hết nhà hiện có hoặc bạn chưa chọn nhà thuê", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Return
        End If
        Dim kh As New KhachHangDTO
        'kh.Makh
        kh.Manha = ddlNha.SelectedValue
        kh.Hoten = txtHoTen.Text
        kh.Cmnd = txtCMND.Text
        kh.Sdt = txtSDT.Text
        kh.NgayThueNha = txtNgaythuenha.Text
        kh.NgayKetThuc = txtNgayketthuc.Text
        KhachHangBUS.KhachHang_Insert(kh)
        GridView_Load()
        ddlNhaLoad()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim kh As New KhachHangDTO
        kh.Makh = GView.GetFocusedRowCellDisplayText("MAKH")
        If ddlNha.Text <> "" Then
            If MessageBox.Show("Bạn có chắc chắn muốn thay đổi nhà cho khách hàng " & kh.Makh, "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
                Return
            End If
        End If
        If Not TestInfo() Then
            Return
        End If
        If Not ThongBao.Agree Then
            Return
        End If
        kh.Manha = GView.GetFocusedRowCellValue("MANHA")
        kh.Hoten = txtHoTen.Text
        kh.Cmnd = txtCMND.Text
        kh.Sdt = txtSDT.Text
        kh.NgayThueNha = txtNgaythuenha.Text
        kh.NgayKetThuc = txtNgayketthuc.Text
        KhachHangBUS.KhachHang_Update(kh)
        GridView_Load()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Not ThongBao.Agree Then
            Return
        End If
        Dim kh As New KhachHangDTO
        kh.Makh = GView.GetFocusedRowCellDisplayText("MAKH")
        kh.Manha = GView.GetFocusedRowCellValue("MANHA")
        KhachHangBUS.KhachHang_Delete(kh)
        GridView_Load()
        ddlNhaLoad()
    End Sub

    Private Sub GView_RowCellClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs) Handles GView.RowCellClick
        ddlNha.SelectedValue = GView.GetFocusedRowCellValue("MANHA")
        txtHoTen.Text = GView.GetFocusedRowCellDisplayText("HOTEN")
        txtCMND.Text = GView.GetFocusedRowCellDisplayText("CMND")
        txtSDT.Text = GView.GetFocusedRowCellDisplayText("SDT")
        txtMaKH.Text = GView.GetFocusedRowCellDisplayText("MAKH")
        txtNgaythuenha.Text = GView.GetFocusedRowCellDisplayText("NGAYTHUENHA")
        txtNgayketthuc.Text = GView.GetFocusedRowCellDisplayText("NGAYKETTHUC")
    End Sub

    Private Sub UCKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GridView_Load()
        ddlNhaLoad()
        ChiTietNha()
        txtNgaythuenha.Text = DateTime.Now.ToShortDateString
        txtNgayketthuc.Text = DateTime.Now.AddYears(1).ToShortDateString
    End Sub

    Sub ChiTietNha()
        If ddlNha.SelectedValue Is Nothing Then
            Return
        End If
        Dim manha As String = ddlNha.SelectedValue.ToString
        Dim sql As String = String.Format("select n.*,l.TENLOAINHA from NHA n, LOAINHA l where l.MALOAINHA = n.MALOAINHA and n.MANHA = '{0}'", manha)
        Dim dt As DataTable = DataProvider.LoadTable(sql)
        If dt.Rows.Count = 0 Then
            Return
        End If
        Dim dr As DataRow = dt.Rows(0)
        txtln.Text = dr("TENLOAINHA")
        txtdt.Text = dr("DIENTICH")
        txtmg.Text = dr("MUCGIA")
        txtdc.Text = dr("DIACHI")
        grc.Text = "Thông tin nhà " + manha
    End Sub
    Private Sub ddlNha_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlNha.SelectedIndexChanged
        ChiTietNha
    End Sub
End Class
